package com.smartystars.quizzer;

import java.io.File;
import java.util.Map;

import com.smartystars.quizzer.R;
import com.smartystars.quizzerServer.DownloadableQuestions;
import com.smartystars.quizzerServer.ExternalStorage;
import com.smartystars.ui.Carousel;
import com.smartystars.ui.CarouselAdapter;
import com.smartystars.ui.CarouselAdapter.OnItemClickListener;
import com.smartystars.ui.CarouselAdapter.OnItemSelectedListener;

import android.os.Bundle;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
import android.widget.Button;

/**
 * 
 * @author Dominik v.d.Heydt
 * 
 */
public class QuizzerStartupActivity extends Activity {
	
    @Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.startuplayout);
		
		 Carousel carousel = (Carousel)findViewById(R.id.carousel);
	        carousel.setOnItemClickListener(new OnItemClickListener(){

				public void onItemClick(CarouselAdapter<?> parent, View view,
						int position, long id) {	
					switch(position){
					case 0:
						Intent myIntent = new Intent(getBaseContext(), SingleplayerActivity.class);
						myIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(myIntent);
						break;
						
					case 1:
						Intent myMPIntent = new Intent(getBaseContext(), MultiplayerStartupActivity.class);
						myMPIntent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
						startActivity(myMPIntent);
						
						break;
						
					case 2:
						
						break;
						
					case 3:
						
						break;
					}
						
				}
	        	
	        });

	        carousel.setOnItemSelectedListener(new OnItemSelectedListener(){

				public void onItemSelected(CarouselAdapter<?> parent, View view,
						int position, long id) {
					
			        
			        
					switch(position){
					case 0:

					
						break;
						
					case 1:
						
						break;
						
					case 2:
						
						break;
						
					case 3:
						
						break;
					}
					
				}

				public void onNothingSelected(CarouselAdapter<?> parent) {
				}
	        	
	        }
	        );
	        
		
		
		
		
		Map<String, File> externalLocations = ExternalStorage
				.getAllStorageLocations();
		File sdCard = externalLocations.get(ExternalStorage.SD_CARD);
		
		File questions = new File(sdCard + "/Quizzer/questions.ser");
		System.out.println(questions);
	
		if(!questions.exists()){
			

			AlertDialog.Builder downloadDialog = new AlertDialog.Builder(this);
			downloadDialog.setTitle("Confirm Question Download");
			downloadDialog.setMessage("No questions found you need to download them...");
			downloadDialog.setPositiveButton("Download", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					// TODO Auto-generated method stub
					downloadQuestions();
					
				}
			});
			
			downloadDialog.setNegativeButton("Dismiss", new DialogInterface.OnClickListener() {
				
				@Override
				public void onClick(DialogInterface dialog, int which) {
					System.exit(0);
					
				}
			});
				downloadDialog.setCancelable(false);
				downloadDialog.show();
			
			}
		
		
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		// Inflate the menu; this adds items to the action bar if it is present.
		getMenuInflater().inflate(R.menu.quizzer_snglstartup, menu);
		return true;
	}
	
	
	public void downloadQuestions(){
		String language = getApplicationContext().getResources().getConfiguration().locale.getDisplayName();
		System.out.println(language);
		
		if(language.contains("Deutsch")){
			
			NetworkThread downloadThread = new NetworkThread("DOWNLOAD","Deutsch",0);
			downloadThread.start();
		}
		
		
	}

}
